package com.briup.jz.web.controller;

import java.util.List;

import javax.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.briup.jz.bean.Product;
import com.briup.jz.bean.extend.ProductExtend;
import com.briup.jz.service.IProductService;
import com.briup.jz.utils.Message;
import com.briup.jz.utils.MessageUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(description="产品信息相关接口")
@Validated
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    @ApiOperation(value = "查询产品信息")
    @ApiImplicitParams({
		@ApiImplicitParam(name="name",value = "产品名称",paramType = "query"),
    })
    @GetMapping("query")
    public Message query(String name,String status,Long productCategoryId){
    	List<Product> list = productService.query(name,status,productCategoryId);
        return MessageUtil.success(list);
    }
    
    //级联查询
    @ApiOperation(value = "级联查询产品信息")
    @GetMapping("queryCascade")
    public Message queryCascade(String name,String status,Long productCategoryId){
        List<ProductExtend> list = productService.queryCascade(name,status,productCategoryId);
        return MessageUtil.success(list);
    }
    
    //分页查询 
    @ApiOperation(value = "分页查询产品信息")//注解依附于方法、属性...，需要在方法之上
    @ApiImplicitParams({
            @ApiImplicitParam(name="page",value = "当前页",paramType = "query",required = true),
            @ApiImplicitParam(name="pageSize",value = "每页大小",paramType = "query",required = true),
            @ApiImplicitParam(name="name",value = "产品名称",paramType = "query"),
            @ApiImplicitParam(name="status",value = "产品状态",paramType = "query"),
            @ApiImplicitParam(name="productCategoryId",value = "分类编号",paramType = "query"),
    })
    @GetMapping("pageQuery")
    public Message pageQuery(int page,int pageSize,String name,String status,Long productCategoryId){
        List<ProductExtend> list = productService.pageQuery(page,pageSize,name,status, productCategoryId);
        return MessageUtil.success(list);
    } 
    
    @ApiOperation(value = "通过id删除产品")
    @DeleteMapping("deleteById")
    @ApiImplicitParams({
    		@ApiImplicitParam(name="id",value = "主键",paramType = "query", required=true),
    })
    public Message deleteById(@NotNull Long id){
        productService.deleteById(id);
        return MessageUtil.success("删除成功");
    }

    @ApiOperation(value = "保存或更新产品信息")


    @ApiImplicitParams({					//后面修改
            @ApiImplicitParam(name="id",value = "主键",paramType = "form",required = true),
            @ApiImplicitParam(name="name",value = "产品名称",paramType = "form"),
            @ApiImplicitParam(name="description",value = "产品介绍",paramType = "form"),
            @ApiImplicitParam(name="status",value = "产品状态",paramType = "form"),
            @ApiImplicitParam(name="productCategoryId",value = "分类编号",paramType = "form"),
            @ApiImplicitParam(name="price",value = "单价",paramType = "form"),
            @ApiImplicitParam(name="photo",value = "图片",paramType = "form"),
    })
    @PostMapping("saveOrUpdate")
    public Message saveOrUpdate(Product product){
        productService.saveOrUpdate(product);
        return MessageUtil.success("更新成功");
    }

    //产品上架
    @ApiOperation(value = "上架")//注解依附于方法、属性...，需要在方法之上
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "产品编号",paramType = "query",required = true),
    })
    @GetMapping("online")
    public Message online(Long id){
        productService.online(id);
        return MessageUtil.success("上架成功");
    }
  //产品下架
    @ApiOperation(value = "下架")//注解依附于方法、属性...，需要在方法之上
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "产品编号",paramType = "query",required = true),
    })
    @GetMapping("offline")
    public Message offline(Long id){
        productService.offline(id);
        return MessageUtil.success("下架成功");
    }
}
